package com.kadron.datamodel;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import com.kadron.common.KUtil;
import com.kadron.common.KeyValue;
import com.kadron.dao.KEmbedDAO;
import com.kadron.entity.Embed;
import com.kadron.library.embed.KEmbedListSearch;

public class KEmbedListModelDataProvider implements KDataProvider {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4707252300895840420L;
	
	private KModelCreator creator;
	private KEmbedDAO embedDAO;
	private KEmbedListSearch search;
	
	public KEmbedListModelDataProvider(KModelCreator creator,KEmbedListSearch search, KEmbedDAO embedDAO) {
		super();
		this.creator = creator;
		this.search = search;
		this.embedDAO = embedDAO;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Long> getItemsByRange(Integer integer, int numberOfRows,
			Object object, boolean b) {
		EntityManager em = embedDAO.getEntityManager();
		Set<KeyValue> params = new HashSet<KeyValue>();
		StringBuilder builder = new StringBuilder("SELECT a.id FROM Embed a WHERE a.active=TRUE ");
		if (!KUtil.isEmpty(search.getName())) {
			builder.append(" AND (a.name_en LIKE :name OR a.name_es LIKE :name OR a.name_sr LIKE :name) ");
			params.add(new KeyValue("name", "%"+search.getName()+"%"));
		}
		if (search.getDate()!= null) {
			builder.append(" AND a.created >= :created ");
			params.add(new KeyValue("created", search.getDate()));
		}
		builder.append("ORDER BY a.id DESC");
		Query query =	em.createQuery(builder.toString());
		for (KeyValue p : params) {
			query.setParameter(p.getKey(), p.getValue());
		}
		query.setFirstResult(integer);
		query.setMaxResults(numberOfRows);
		return query.getResultList();
	}

	@Override
	public KModel getItemById(Long id, KModelCreator creator) {
		return embedDAO.find(id, Embed.class);
	}

	@Override
	public int getRowCount() {
		EntityManager em = embedDAO.getEntityManager();
		Set<KeyValue> params = new HashSet<KeyValue>();
		StringBuilder builder = new StringBuilder("SELECT COUNT(a.id) FROM Embed a  WHERE a.active=TRUE ");
		if (!KUtil.isEmpty(search.getName())) {
			builder.append(" AND (a.name_en LIKE :name OR a.name_es LIKE :name OR a.name_sr LIKE :name) ");
			params.add(new KeyValue("name", "%"+search.getName()+"%"));
		}
		if (search.getDate()!= null) {
			builder.append(" AND a.created >= :created ");
			params.add(new KeyValue("created", search.getDate()));
		}
		Query query = em.createQuery(builder.toString());
		for (KeyValue p : params) {
			query.setParameter(p.getKey(), p.getValue());
		}
		Long count = (Long) query.getSingleResult();
		return (count == null) ? 0 : count.intValue() ;
	}

	@Override
	public boolean hasItemWithId(Long id) {
		return embedDAO.exists(id, Embed.class);
	}

	@Override
	public KModelCreator getCreator() {
		return creator;
	}
	
	public KEmbedListSearch getSearch() {
		return search;
	}

	public void setSearch(KEmbedListSearch search) {
		this.search = search;
	}

}
